package net.controller;

import net.pojo.Order;
import net.pojo.User;
import net.service.CartService;
import net.service.CategoryService;
import net.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author admin
 * @Date 2019/4/25 10:47
 */
@Controller
@RequestMapping("/userorder")
public class OrdersController {
    @Autowired
    OrderService orderService;
    @Autowired
    CartService cartService;
    @RequestMapping({"/createOrder","/paymentOrder"})
    public String createOrder(HttpSession session, Model model, Order order){
        if (order.getId()==null){
            User user = (User) session.getAttribute("user");
            int resultCount = orderService.createOrder(order,user);
        }
        model.addAttribute("order",order);
        return "/ali/alipay.trade.page.pay.jsp";
    }

    @RequestMapping("/myOrder")
    public String myOrder(Model model,HttpSession session){
        User user = (User) session.getAttribute("user");
        List<Order> clist = orderService.findByUid(user.getId());
        int count = clist.size();
        model.addAttribute("count", count);
        model.addAttribute("clist",clist);
        return "/client/orderlist.jsp";
    }
    @RequestMapping("/orderDetails")
    public String showOrderDetails(Model model,String id){
        Order orderDeatils = orderService.findByOrderId(id);
        model.addAttribute("order",orderDeatils);
        return "/client/orderInfo.jsp";
    }
    @RequestMapping("/payReturn")
    public String payReturn(Model model,String out_trade_no){
        int resultCount =  orderService.updatePayState(out_trade_no,1);
        model.addAttribute("msg","付款成功、、、");
        return "/client/createOrderSuccess.jsp";
    }

    @RequestMapping("/deleteById")
    public String deleteById(String id,Model model){
        int resultCount = orderService.deleteById(id);
        return "/client/delOrderSuccess.jsp";
    }
}
